﻿using Com.JunCaiSoft.AutoMapper;
using Com.JunCaiSoft.Common.Controller;
using Com.JunCaiSoft.Common.Enums;
using Com.JunCaiSoft.Common.ExchangeData;
using Com.JunCaiSoft.Common.Model;
using Com.JunCaiSoft.Hr.Entity;
using Com.JunCaiSoft.Hr.IDao;
using Com.JunCaiSoft.Hr.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Com.JunCaiSoft.API.Hr.Controllers
{
    /// <summary>
    /// 公司管理模块
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class CompanyController : BaseController<CompanyView,CompanyItem,Company, Guid>
    {
        public CompanyController(ICompanyDao companydao) : base(companydao)
        {

        }
        /// <summary>
        /// 通过公司Code查询公司
        /// </summary>
        /// <param name="CompanyCode"></param>
        /// <returns></returns>
        [HttpGet]
        public virtual async Task<ActionResult> QueryByCode(String CompanyCode)
        {
            try
            {
                var list = DaoAsync.GetAsync(x => x.NumCode == CompanyCode).Result;
                return await Task.Run(() => Content(JsonConvert.SerializeObject(list)));
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        /// <summary>
        /// 按公司ID单个删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpDelete]
        public override async Task<ActionResult> DeleteSingleAsync(Guid Id)
        {
            try
            {
                await DaoAsync.dbContext.Database.BeginTransactionAsync();
                int result = await DaoAsync.DeleteListAsync<Company>(x => x.Id == Id);

                return await Task.Run(() => Content(JsonConvert.SerializeObject(result)));
            }
            catch (Exception e)
            {
                await DaoAsync.dbContext.Database.RollbackTransactionAsync();
                throw e;
            }
            finally
            {
                DaoAsync.dbContext.Database.CommitTransaction();
            }
        }
        /// <summary>
        /// 按公司ID批量删除
        /// </summary>
        /// <param name="DeleteList"></param>
        /// <returns></returns>
        [HttpPatch]
        public virtual async Task<ActionResult> DeleteList(IEnumerable<Guid> DeleteList)
        {
            try
            {
                await DaoAsync.dbContext.Database.BeginTransactionAsync();

                int result = await DaoAsync.DeleteListAsync<Company>(x => DeleteList.Contains(x.Id));

                return await Task.Run(() => Content(JsonConvert.SerializeObject(result)));
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                DaoAsync.dbContext.Database.CommitTransaction();
            }
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize"></param>
        /// <param name="keyword">关键字</param>
        /// <returns></returns>
        [HttpGet]
        public virtual async Task<ActionResult> ListByPageAsync(int pageIndex, int pageSize, String keyword)
        {
            try
            {
                // ,

                if (String.IsNullOrEmpty(keyword))
                {
                    keyword = "";
                }

                var queryList = await DaoAsync.ListByPageAsync(x => x.Name.Contains(keyword) || x.NickName.Contains(keyword), x => x.CreateTime, pageIndex, pageSize, OrderbyType.ase);

                var list = new List<CompanyItem>();

                queryList.items.ToList().ForEach(item => list.Add(item.MapTo<CompanyItem>()));

                var resultList = new QueryData<CompanyItem>() { totalCount = queryList.totalCount, items = list.OrderByDescending(x => x.CreateTime) };

                return await Task.Run(() => Content(JsonConvert.SerializeObject(resultList)));
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        /// <summary>
        /// 创建公司
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public override async Task<ActionResult> CreateAsync(CompanyView entity)
        {
            try
            {
                DaoAsync.dbContext.Database.BeginTransaction();

                var saveone = entity.MapTo<Company>();
                var result = await DaoAsync.InsertAsync(saveone);

                DaoAsync.dbContext.Database.CommitTransaction();
                return await Task.Run(() => Content(JsonConvert.SerializeObject(result)));
            }
            catch (Exception e)
            {
                throw e;
            }

        }
        [HttpPut]
        public override async Task<ActionResult> UpdateAsync(UpdateEntity<CompanyView> entityModel)
        {
            DaoAsync.dbContext.Database.BeginTransaction();

            var saveone = entityModel.entity.MapTo<Company>();
            var result = await DaoAsync.UpdateAsync(saveone, entityModel.Id);

            DaoAsync.dbContext.Database.CommitTransaction();
            return await Task.Run(() => Content(JsonConvert.SerializeObject(result)));
        }
    }
}
